//
//  HomeViewController.swift
//  MQLWeibo
//
//  Created by MQL-IT on 2016/10/23.
//  Copyright © 2016年 MQL-IT. All rights reserved.
//

import UIKit
//定义全局常量,尽量使用私有,仅在本文件中使用
private let cellId = "cellId"

class HomeViewController: BaseViewController {
    //懒加载微博数据数组,懒加载无需考虑实例化和解包;
//    fileprivate lazy var statusList = [String]()
    /// 列表视图模型
    fileprivate lazy var listViewModel = StatusListViewModel()
    //加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullup: isPullUp) { (isSuccess, shouldRefresh) in
            printLog(message: "加载数据结束")
            //加载数据结束,结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullUp = false
            //刷新表格
            if shouldRefresh == true {
                self.tableView?.reloadData()
            }
        }
        
    }
    
    //显示好友
    @objc fileprivate func showFriends() {
        print(#function)
        let vc = TestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}
//MARK: - 表格数据源方法,具体的数据源方法,不需要super
extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
         let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! QLStatusCell
        
        let vm = listViewModel.statusList[indexPath.row]
        //2.设置cell
        cell.viewModel = vm
    
        //3.返回cell
        return cell
    }
    
    
    
}


//MARK: - 设置界面
extension HomeViewController {
    //重写父类方法
    override func setupTableView() {
        
    
        super.setupTableView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UINib(nibName: "QLStatusNormalCell", bundle: nil), forCellReuseIdentifier: cellId)
        
        //设置行高
        tableView?.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView?.estimatedRowHeight = 300;
        //取消分割线
        tableView?.separatorStyle = .none
        //点击状态
        
        
        setupNavTitle()
    }
    
    /// 设置导航栏标题
    private func setupNavTitle() {
        let title = NetWorkManager.shared.userAccount.screen_name
        let button = QLTitleButton.init(title: title)
        navItem.titleView = button
        button.isSelected = !button.isSelected
        button.addTarget(self, action: #selector(didClickTitleBtn), for: .touchUpInside)
    }
    
    @objc func didClickTitleBtn(btn: UIButton) {
        // 设置选中状态
        btn.isSelected = !btn.isSelected
    }
}






